//
//  DLSingleSelectTC.m
//  delongzhitou
//
//  Created by Mr.Wang on 2018/11/30.
//  Copyright © 2018 delongzhitou. All rights reserved.
//

#import "DLSingleSelectTC.h"
#import "DLMachineModel.h"

@interface DLSingleSelectTC()

@property (nonatomic, strong) UIView *singleSelectBgView;

@end

@implementation DLSingleSelectTC


- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self layoutsubControls];
        self.backgroundColor = kDLMachineTableAndCellBgColor;
//        self.backgroundColor = kColorRGB(240, 240, 240);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}
- (void) layoutsubControls {
    
    UIView *singleSelectBgView = [UIView new];
    singleSelectBgView.frame = CGRectMake(kMachineSingleSelectBgViewLeftMargin, 0, kScreenWidth - kMachineSingleSelectBgViewLeftMargin *2, 0);
    singleSelectBgView.backgroundColor = [UIColor whiteColor];
    singleSelectBgView.tag = 1000;
    [self.contentView addSubview:singleSelectBgView];
    self.singleSelectBgView = singleSelectBgView;
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.singleSelectBgView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerTopRight | UIRectCornerBottomRight cornerRadii:CGSizeMake(17, 17)];
    
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = maskPath.bounds;
    maskLayer.path = maskPath.CGPath;
    self.singleSelectBgView.layer.mask  = maskLayer;
}
- (void) setSingleSelectModel:(DLMachineModel *)singleSelectModel {
    _singleSelectModel = singleSelectModel;
    [self.singleSelectBgView resetHeight:30 *singleSelectModel.resultArr.count + kMachineSingleSelectControlEdgeInset *2];
    
    
    [self.singleSelectBgView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    NSMutableArray *stirgnWidthArray = [NSMutableArray array];
    
    NSArray *dataArr = singleSelectModel.resultArr;
    for (NSInteger i = 0; i < dataArr.count; i++) {
        NSDictionary *dic = dataArr[i];
        UILabel *titleLabel = [UILabel new];
        titleLabel.frame = CGRectMake(kMachineSingleSelectControlEdgeInset, kMachineSingleSelectControlEdgeInset + 30 * i, self.width - kMachineSingleSelectControlEdgeInset *2, 30);
        titleLabel.textAlignment = NSTextAlignmentLeft;
        titleLabel.textColor = kColorRGB(60, 163, 242);
        titleLabel.font = kBoldFont(14);
        titleLabel.numberOfLines = 1;
        titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        titleLabel.tag = 100 + i;
        titleLabel.userInteractionEnabled = YES;
        [self.singleSelectBgView addSubview:titleLabel];
        NSString *labelText =[NSString stringWithFormat:@"%ld.%@", i + 1, dic[@"showtext"]];
        titleLabel.text = labelText;
        
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(titleLabelTapGes:)];
        [titleLabel addGestureRecognizer:tapGes];
        
        
        //计算每条消息文字的宽度
        CGFloat stringWidth = [labelText getValue4WidWithHeight:20 andFont:kBoldFont(14)];
        [titleLabel resetWidth:stringWidth];
        
        [stirgnWidthArray addObject:[NSNumber numberWithFloat:stringWidth]];
    }
    
    CGFloat maxValue = [[stirgnWidthArray valueForKeyPath:@"@max.floatValue"] floatValue];
    
    [self.singleSelectBgView resetWidth:maxValue + kMachineSingleSelectControlEdgeInset *2];
}
- (void) titleLabelTapGes:(UITapGestureRecognizer *)tapGes {
    
    if (self.singleSelectTitleLabelTapBlock) {
        self.singleSelectTitleLabelTapBlock(tapGes.view.tag - 100);
    }
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}



@end
